package com.spc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spc.bean.Bis_sms_record;
import com.spc.miaodi.AccountInfo;
import com.spc.service.Bis_sms_recordService;

@Controller
@RequestMapping("bissms")
public class Bis_sms_recordController {
 
	@Autowired
	private Bis_sms_recordService bis_sms_recordService;
	@RequestMapping("/list")
	public String list(Model model,HttpServletRequest request,@RequestParam(value="page",defaultValue="1")int page){
		Map<String, Object> map = initMap(request);
		PageHelper.startPage(page, 4);
		List<Bis_sms_record> list=this.bis_sms_recordService.listAll(map);
		PageInfo<Bis_sms_record> info=new PageInfo<>(list);
		model.addAttribute("info",info);
		return "spc/Bis_sms_recordList";
	}
	 
	private Map<String, Object> initMap(HttpServletRequest request) {
		String mobile=request.getParameter("mobile");//收件人
		String content=request.getParameter("content");//短息内容
		String create_time1=request.getParameter("create_time1");//创建时间1
		String create_time2=request.getParameter("create_time2");//创建时间2
		String sms_type=request.getParameter("sms_type");//短息类型
		String status=request.getParameter("status");//状态
		String send_time1=request.getParameter("send_time1");//发起时间1
		String send_time2=request.getParameter("send_time2");//发起时间2
		Map<String, Object> map=new HashMap<>();
		map.put("mobile",mobile );//收件人
		map.put("content",content );//短息内容
		map.put("create_time1", create_time1);//创建时间1
		map.put("create_time2", create_time2);//创建时间2
		map.put("sms_type",sms_type );//短信类型
		map.put("status", status);//状态
		map.put("send_time1",send_time1 );//发起时间1
		map.put("send_time2", send_time2);//发起时间2
        return map;
	}
	
	@RequestMapping("toadd")
	public String toadd(){
		return "spc/Bis_sms_recordadd";
	}
	
	@RequestMapping("delete")
	public String delete(String ids){
		bis_sms_recordService.delete(ids);
		return "redirect:/bissms/list";
	}
	
	@RequestMapping("save")
	public String insertlist(Bis_sms_record record,HttpServletRequest request){
		String mobile=request.getParameter("mobile");
		String content =(int)((Math.random()*9+1)*100000)+"";
		record.setContent(content);
		record.setRecord_id(UUID.randomUUID().toString());
		record.setSms_type("1");
		 try{
			// 获取开发者账号信息
			 AccountInfo.execute();
			// 验证码通知短信接口   手机号和发送短信的内容
			 com.spc.miaodi.IndustrySMS.execute(mobile, content);
			 record.setStatus("1");
		 }catch(Exception e){
			 record.setStatus("0");
			 e.printStackTrace();
		 }
		this.bis_sms_recordService.save(record);
		return "redirect:/bissms/list";
	}
}
